package cn.lanchengkeji.datafillingsystem.service.impl;


import cn.lanchengkeji.datafillingsystem.common.ResponseBean;
import cn.lanchengkeji.datafillingsystem.common.StatusCode;
import cn.lanchengkeji.datafillingsystem.entity.Operation;
import cn.lanchengkeji.datafillingsystem.mapper.OperationMapper;
import cn.lanchengkeji.datafillingsystem.service.IOperationService;
import cn.lanchengkeji.datafillingsystem.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 操作权限 服务实现类
 * </p>
 *
 * @author Yixu
 * @since 2018-11-29
 */
@Service
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements IOperationService {
    @Override
    public List<String> getPermissionCodesByRoles(List roles) {
        List<String> permissions = baseMapper.selectPermissionCodesByRoles(roles);
        return permissions;
    }

    /**
     * 添加
     *
     * @param operation
     * @return
     * @throws Exception
     * @author yuhao
     * @version 2018-12-05 17:26
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseBean insertOperation(Operation operation) throws Exception {

        if(Utils.isEmpty(operation)){
            return new ResponseBean(StatusCode.IS_NULL, operation);
        }

        List<Operation> searchOperations;

        // 验证权限名称是否重复
        if(Utils.notEmpty(operation.getOperationName())){
            QueryWrapper<Operation> condition = new QueryWrapper<>();
            condition.eq(Operation.OPERATION_NAME, operation.getOperationName());
            searchOperations = this.list(condition);
            if (searchOperations.size() > 0) {
                return new ResponseBean(StatusCode.OPERATION_NAME_REPEAT, searchOperations);
            }
        }

        // 验证权限格式是否重复
//        if(Utils.notEmpty(operation.getPermissionCode())){
//            QueryWrapper<Operation> condition = new QueryWrapper<>();
//            condition.eq("permission_code", operation.getPermissionCode());
//            searchOperations = this.list(condition);
//            if (searchOperations.size() > 0) {
//                return new ResponseBean(StatusCode.OPERATION_CODE_REPEAT, searchOperations);
//            }
//        }

        if(this.save(operation)){
            return new ResponseBean(StatusCode.OK,operation);
        }else{
            return new ResponseBean(StatusCode.OK_FAIL,operation);
        }

    }

    /**
     * 修改
     *
     * @param operation
     * @return
     * @throws Exception
     * @author yuhao
     * @version 2018-12-05 17:26
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseBean updateOperation(Operation operation) throws Exception {

        if(Utils.isEmpty(operation)||Utils.isEmpty(operation.getId())){
            return new ResponseBean(StatusCode.IS_NULL, operation);
        }

        List<Operation> searchOperations;

        // 验证除自己外权限名称是否重复
        if(Utils.notEmpty(operation.getOperationName())){
            QueryWrapper<Operation> condition = new QueryWrapper<>();
            condition.eq(Operation.OPERATION_NAME, operation.getOperationName()).ne("id",operation.getId());
            searchOperations = this.list(condition);
            if (searchOperations.size() > 0) {
                return new ResponseBean(StatusCode.OPERATION_NAME_REPEAT, searchOperations);
            }
        }

        // 验证除自己外权限格式是否重复
//        if(Utils.notEmpty(operation.getPermissionCode())){
//            QueryWrapper<Operation> condition = new QueryWrapper<>();
//            condition.eq("permission_code", operation.getPermissionCode()).ne("id",operation.getId());
//            searchOperations = this.list(condition);
//            if (searchOperations.size() > 0) {
//                return new ResponseBean(StatusCode.OPERATION_CODE_REPEAT, searchOperations);
//            }
//        }

        if(this.saveOrUpdate(operation)){
            return new ResponseBean(StatusCode.OK,operation);
        }else{
            return new ResponseBean(StatusCode.OK_FAIL,operation);
        }
    }

    /**
     * 删除
     *
     * @param operation
     * @return
     * @throws Exception
     * @author yuhao
     * @version 2018-12-05 17:26
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseBean deleteOperation(Operation operation) throws Exception {

        // 参数不存在
        if (Utils.isEmpty(operation) || Utils.isEmpty(operation.getId())) {
            return new ResponseBean(StatusCode.NOT_FOUND, null);
        }

        // 判断当前权限记录是否已不存在
        Operation old_operation = this.getById(operation.getId());
        if (Utils.isEmpty(old_operation)) {
            return new ResponseBean(StatusCode.OPERATION_NOT_FOUND, null);
        }

        // 删除
        if (this.removeById(old_operation)) {
            return new ResponseBean(StatusCode.OK, old_operation);
        } else {
            return new ResponseBean(StatusCode.OK_FAIL, old_operation);
        }
    }

    /**
     * 按id查看单条记录
     *
     * @param operation
     * @return
     * @author yuhao
     * @version 2018-12-05 17:26
     */
    @Override
    public Operation selectOneOperation(Operation operation) {
        return this.getById(operation);
    }

    /**
     * 按条件分页查询多条数据
     *
     * @param page
     * @param condition
     * @return
     * @author yuhao
     * @version 2018-12-05 17:26
     */
    @Override
    public Page<Operation> pageOperation(Page<Operation> page, Operation condition) {
        List<Operation> usersList = this.baseMapper.findOperations(page,condition);
        page.setRecords(usersList);

        return page;
    }

    /**
     * 根据角色id查询操作权限
     *
     * @param roleId
     * @return
     * @author yuhao 2018-12-07 15:56
     */
    @Override
    public List<Operation> selectOperationsByRoleId(Integer roleId) {
        return baseMapper.selectOperationsByRoleId(roleId);
    }

}
